package test;


import com.google.common.primitives.Chars;
import com.leyou.item.dto.SpecGroupDTO;
import org.junit.Test;

import java.io.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import static rx.internal.operators.NotificationLite.getValue;

//stream
public class Thvdch   {
    public static void main(String[] args) {
        List<SpecGroupDTO> list = new LinkedList<>();
        List<SpecGroupDTO> list2 = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            SpecGroupDTO specGroupDTO = new SpecGroupDTO();
            specGroupDTO.setId(i * 999L);
            specGroupDTO.setName("kimi" + i);
            list.add(specGroupDTO);
            list2.add(specGroupDTO);
        }
//实现了集合------>流------>集合    前对象处理；return==对象转换；分类joining toList
        String collect = list.stream().map(xx -> xx.getId() + "").collect(Collectors.joining("-"));
        System.out.println(collect);
        //list.forEach(ss -> System.out.println(ss.getCid()));
        list2.forEach(yy -> {
            yy.setCid(4444l);
        });//返回void

    }


    @Test
    public void test() throws FileNotFoundException, UnsupportedEncodingException, InterruptedException {
        int[] arr = {7, 2, 3, 4, 4, 3, 4, 8, 6, 3, 6, 9, 8, 2, 3, 6, 9, 2, 8, 3, 9, 2, 3, 49, 2, 3, 8, 9, 8, 23, 6, 9, 23, 5, 6, 9, 8, 235, 69, 2389283};
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {

            Integer integer = map.get(arr[i]);
            if (integer == null) {
                map.put(arr[i], 1);
            } else {
                map.put(arr[i], integer + 1);
            }
        }
        HashMap<Integer, Integer> sort = sort(map);
        sort.forEach((x, y) -> System.out.println(x + "出现：" + y + "次"));

    }

    public static HashMap<Integer, Integer> sort(Map<Integer, Integer> map) {
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        ArrayList<Map.Entry<Integer, Integer>> arr = new ArrayList<>(entries);
        arr.sort(new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });
        LinkedHashMap<Integer, Integer> integerIntegerLinkedHashMap = new LinkedHashMap<>();
        arr.forEach(x -> integerIntegerLinkedHashMap.put(x.getKey(), x.getValue()));
        return integerIntegerLinkedHashMap;
    }

    public volatile Integer count = 0;
    public Boolean b = false;

    class Couu {
        final Lock lock = new ReentrantLock();
        public String name;
        public Runnable ru1 = new Runnable() {
            @Override
            public void run() {
              /* try {
                   if (lock.tryLock(1000, TimeUnit.MICROSECONDS)){
                       System.out.println("dedao11111");
                   }else {
                       System.out.println("meide000000");
                   }
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }*/
                synchronized (new Integer(1).getClass()) {
                    while (count < 10) {
                        System.out.println(count++);
                        System.out.println(Thread.currentThread().getName() + "        ru1" + name);

                    }
                }
            }
        };
        public Runnable ru2 = new Runnable() {
            @Override
            public void run() {

                synchronized (new Integer(1).getClass()) {

                    while (count < 20) {
                        System.out.println(count++);

                        System.out.println(Thread.currentThread().getName() + "        ru2" + name);

                    }
                }
            }
        };
    }

    @Test
    public void uuu() throws InterruptedException {

//    	String st="javac";
        String s1 = "jav";
        s1 = s1 + "ac";
        //相当于是new了一个字符串对象"javac"，放入到堆内存中。
        //s1="jav"+"ac"是等价于s1="javac"的，并没有在堆内存中new一个对象。
        System.out.println(s1.intern() == s1);

        String s2 = "ja";
        s2 = s2 + "va";
        System.out.println(s2.intern() == s2);

        String s3 = new String("abc");
        System.out.println(s3.intern() == s3);
        int a = new Integer(5);
        System.out.println(new Integer(5) == a);
    }

    private static final int SIZE = 9;
    private int[][] finalData = {
            {1, 0, 0, 5, 0, 0, 4, 0, 0},
            {0, 0, 9, 0, 3, 0, 0, 0, 0},
            {0, 7, 0, 0, 0, 8, 0, 0, 5},
            {0, 0, 1, 0, 0, 0, 0, 3, 0},
            {8, 0, 0, 6, 0, 0, 5, 0, 0},
            {0, 9, 0, 0, 0, 7, 0, 0, 8},
            {0, 0, 4, 0, 2, 0, 0, 1, 0},
            {2, 0, 0, 8, 0, 0, 6, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 2},
    };

    private Map<String, List<Integer>> repeatMap = new HashMap<>();
    private List<String> historyPoint = new ArrayList<>(81);
    private List<Integer> baseNumber = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

    /**
     * 去除重复的数
     *
     * @param x 坐标x
     * @param y 坐标y
     * @return 该位置能预选的数
     */
    private List<Integer> removeRepeatNumber(int x, int y) {
        // 候选的数
        List<Integer> result = new ArrayList<>(baseNumber);
        // 行、列踢除
        List<Integer> waitRemoveNumber = new ArrayList<>();
        for (int i = 0; i < SIZE; ++i) {
            if (finalData[i][y] != 0) {
                waitRemoveNumber.add(finalData[i][y]);
            }
            if (finalData[x][i] != 0) {
                waitRemoveNumber.add(finalData[x][i]);
            }
        }
        // 九宫内踢除
        final int x1 = x / 3 * 3;
        final int y1 = y / 3 * 3;
        for (int i = x1; i < x1 + 3; ++i) {
            for (int j = y1; j < y1 + 3; ++j) {
                if (finalData[i][j] != 0) {
                    waitRemoveNumber.add(finalData[i][j]);
                }
            }
        }
        // 移除已经存在的数
        result.removeAll(waitRemoveNumber);
        // 移除运算过种中试探过的数
        List<Integer> removeList = repeatMap.get(x + "," + y);
        if (removeList != null) {
            result.removeAll(removeList);
        }
        return result;
    }

    public void runApplication() {
        int x = 0;
        int y = 0;
        boolean flag = true;
        int loopCount = 0;
        while (flag) {
            loopCount++;
            if (finalData[x][y] == 0) {
                String key = x + "," + y;
                // 找出待预选的数
                List<Integer> waitNumber = this.removeRepeatNumber(x, y);
                // 若有则进行填充
                if (!waitNumber.isEmpty()) {
                    // 记录历史走过的点
                    historyPoint.add(key);
                    List<Integer> repeatList = repeatMap.get(key);
                    if (repeatList == null) {
                        repeatList = new ArrayList<>();
                        repeatMap.put(key, repeatList);
                    }
                    finalData[x][y] = waitNumber.get(0);
                    repeatList.add(finalData[x][y]);
                } else if (!historyPoint.isEmpty()) {
                    // 进行回退
                    List<Integer> repeatList = repeatMap.get(key);
                    if (repeatList != null) {
                        repeatList.clear();
                    }
                    String[] history = historyPoint.remove(historyPoint.size() - 1).split(",");
                    x = Integer.parseInt(history[0]);
                    y = Integer.parseInt(history[1]);
                    finalData[x][y] = 0;
                    continue;
                }
            }
            // 顺序往下
            y++;
            if (y >= SIZE) {
                y = 0;
                x++;
            }
            // 假定退出的条件
            if (x >= SIZE) {
                flag = false;
            }
        }
        System.out.println("loop count: " + loopCount);
    }

    public void printResult() {
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                System.out.print(finalData[i][j] + "\t");
            }
            System.out.println();
        }
    }

    @Test
    public void tyyt() {
        String str = "abcaadfgsdhsghfsbcbcabc";
        char[] chars = str.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i])) {
                map.put(chars[i], map.get(chars[i]) + 1);
            } else {
                map.put(chars[i], 1);
            }
        }
        map.forEach((key, value) -> System.out.println("字符" + key + "共" + value + "个"));
        Integer as = null;
        //as.toString();
        String s = String.valueOf(as);
        System.out.println(as);

    }


    public static boolean isBlank(CharSequence cs) {
        int strLen;

        if ((cs == null) || ((strLen = cs.length()) == 0))
            return true;
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public String jdjd() throws BadUrlException {
        String jjdjjd = readDataFromUrl("jjdjjd");


        return "";
    }

    public String readDataFromUrl(String url)
            throws BadUrlException {
        if (isBlank(url)) {
            throw new BadUrlException("Bad URL: " + url);
        }

        String data = null;
        //read lots of data over HTTP and return
        //it as a String instance.

        return data;
    }

    public class BadUrlException extends Exception {
        public BadUrlException(String s) {
            super(s);
        }
    }

    @Test
    public void sumTest() {
     int x=5;
        int p=(x++)/3;
        System.out.println((x++)/3);
        System.out.println(p);
    }

    @Test
    public void penTest() {
        double a = 3;
        double b = 2;
        double c = 0.5;
        for (int i = 0; i <= 100 / a; i++) {
            for (int i1 = 0; i1 <= (100 - a * i) / 2; i1++) {
                if ((100 - i - i1) * c + i * a + i1 * b == 100)
                    System.out.println(i + " " + i1 + " " + (100 - i - i1));
            }
        }
    }
}
